package com.pineapple.module.web.remove;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.pineapple.module.core.remove.domain.Remove;
import com.pineapple.module.core.remove.domain.RemoveCriteria;
import com.pineapple.module.core.remove.domain.RemoveModel;
import com.pineapple.module.core.remove.service.RemoveManager;
import com.pineapple.module.util.connection.DBCPConnection;

public class RemoveAction extends ActionSupport implements ModelDriven<RemoveModel> {

	private static final long serialVersionUID = -3661000161146714152L;
	RemoveModel model = new RemoveModel();


	public RemoveAction(){
		try(Connection conn = new DBCPConnection().getConnection()){
			RemoveManager manager = new RemoveManager();
			model.setLstArea(manager.searchComboPlantAndArea(conn));
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public String init(){
		SimpleDateFormat thaiFormat = new SimpleDateFormat("dd/MM/yyyy",new Locale("en","EN"));
		Date date = new Date();
		String sysDate = thaiFormat.format(date);
		model.getCriteria().setRemoveDateFrom(sysDate);
		model.getCriteria().setRemoveDateTo(sysDate);


		return "search";
	}


	public String search(){
		String criteriaKey = "";
		HttpSession session = ServletActionContext.getRequest().getSession();
		RemoveCriteria criteria = model.getCriteria();

		try(Connection conn = new DBCPConnection().getConnection()){
			RemoveManager manager = new RemoveManager();

			if (model.getCriteriaKey() != null && !model.getCriteriaKey().equals("")) {
				criteria = (RemoveCriteria) session.getAttribute(model.getCriteriaKey());
				int startrecord = model.getStart();
				criteria.setStart(startrecord);
				criteriaKey = model.getCriteriaKey();
				model.setCriteria(criteria);
			} else {
				Calendar calendar = Calendar.getInstance();
				criteriaKey = String.valueOf(calendar.getTimeInMillis());
				criteria.setCriteriaKey(criteriaKey);
			}
			model.setLstRemoveSearch(manager.search(model.getCriteria(), conn));
			if (model.getLstRemoveSearch().size() == 0) {
				ServletActionContext.getRequest().setAttribute("message",getText("0002"));
			}
			model.setCriteria(criteria);
			model.setCriteriaKey(criteriaKey);
			session.setAttribute(criteriaKey, criteria);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return"search";
	}

	public String  gotoAdd(){
		model.setPage("add");
		return "addEdit";
	}

	public String add(){
		try(Connection conn = new DBCPConnection().getConnection()){
			RemoveManager manager = new RemoveManager();
			manager.add(conn, model.getRemove());
			model.setRemove(new Remove());
			model.setLstArea(manager.searchComboPlantAndArea(conn));
			ServletActionContext.getRequest().setAttribute("message",getText("0004"));
		}catch(Exception ex){
			ServletActionContext.getRequest().setAttribute("message",getText("00011"));
			ex.printStackTrace();
		}
		return "addEdit";
	}
	public String edit(){
		try(Connection conn = new DBCPConnection().getConnection()){
			RemoveManager manager = new RemoveManager();
			manager.edit(conn, model.getRemove());
			model.setRemove(new Remove());
			model.setLstArea(manager.searchComboPlantAndArea(conn));
			ServletActionContext.getRequest().setAttribute("message",getText("0006"));
		}catch(Exception ex){
			ServletActionContext.getRequest().setAttribute("message",getText("0005"));
			ex.printStackTrace();
		}
		return "searchDo";
	}

	public String searchById(){
		try(Connection conn = new DBCPConnection().getConnection()){
			int id = 0;
			if(ServletActionContext.getRequest().getParameter("removeId") != null){
				id = Integer.parseInt(ServletActionContext.getRequest().getParameter("removeId"));
			}
			if(ServletActionContext.getRequest().getParameter("page") != null){
				String page = ServletActionContext.getRequest().getParameter("page");
				model.setPage(page);
				RemoveManager manager = new RemoveManager();
				model.setRemove(manager.searchById(conn,id));
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return "addEdit";
	}

	public String delete(){
		String ids = "";
		if(ServletActionContext.getRequest().getParameter("removeIds") != null){
			ids = ServletActionContext.getRequest().getParameter("removeIds");
		}
		try(Connection conn = new DBCPConnection().getConnection()){
				RemoveManager manager = new RemoveManager();
				manager.delete(conn,ids);
				ServletActionContext.getRequest().setAttribute("message",getText("0009"));
		}catch(Exception ex){
			ex.printStackTrace();
			ServletActionContext.getRequest().setAttribute("message",getText("0010"));
		}
		return "searchDo";
	}

	public String clear(){
		model.setCriteria(new RemoveCriteria());
		return "search";
	}
	@Override
	public RemoveModel getModel() {
		// TODO Auto-generated method stub
		return model;
	}








}
